@echo off
setlocal enabledelayedexpansion
chcp 65001 >nul
echo 🔍 MongoDB Sync Diagnosis Tool (Windows)
echo ======================================

REM 检查 Docker 容器状态
echo.
echo [INFO] 1. Checking Docker container status...
docker-compose -f docker-compose-fixed.yml ps

REM 检查网络连接
echo.
echo [INFO] 2. Checking network connectivity...
docker-compose -f docker-compose-fixed.yml exec mongodb-source ping -n 3 mongodb-target >nul 2>&1
if %errorlevel% equ 0 (
    echo [SUCCESS] Network connectivity between containers is OK
) else (
    echo [ERROR] Network connectivity between containers failed
)

REM 检查源数据库
echo.
echo [INFO] 3. Checking source database...
docker-compose -f docker-compose-fixed.yml exec mongodb-source mongosh --username admin --password password1 --authenticationDatabase admin --eval "db.adminCommand('ping')" >nul 2>&1
if %errorlevel% equ 0 (
    echo [SUCCESS] Source database is accessible
    
    REM 检查副本集状态
    for /f "tokens=*" %%i in ('docker-compose -f docker-compose-fixed.yml exec mongodb-source mongosh --username admin --password password1 --authenticationDatabase admin --quiet --eval "rs.status().ok"') do set RS_STATUS=%%i
    if "!RS_STATUS!"=="1" (
        echo [SUCCESS] Source replica set is healthy
    ) else (
        echo [WARNING] Source replica set may have issues
    )
    
    REM 检查测试数据
    for /f "tokens=*" %%i in ('docker-compose -f docker-compose-fixed.yml exec mongodb-source mongosh --username admin --password password1 --authenticationDatabase admin --quiet --eval "db.getSiblingDB('testdb').users.countDocuments()"') do set USER_COUNT=%%i
    echo [INFO] Source users count: !USER_COUNT!
    
) else (
    echo [ERROR] Source database is not accessible
)

REM 检查目标数据库
echo.
echo [INFO] 4. Checking target database...
docker-compose -f docker-compose-fixed.yml exec mongodb-target mongosh --username admin --password password2 --authenticationDatabase admin --eval "db.adminCommand('ping')" >nul 2>&1
if %errorlevel% equ 0 (
    echo [SUCCESS] Target database is accessible
    
    REM 检查副本集状态
    for /f "tokens=*" %%i in ('docker-compose -f docker-compose-fixed.yml exec mongodb-target mongosh --username admin --password password2 --authenticationDatabase admin --quiet --eval "rs.status().ok"') do set RS_STATUS=%%i
    if "!RS_STATUS!"=="1" (
        echo [SUCCESS] Target replica set is healthy
    ) else (
        echo [WARNING] Target replica set may have issues
    )
    
    REM 检查测试数据
    for /f "tokens=*" %%i in ('docker-compose -f docker-compose-fixed.yml exec mongodb-target mongosh --username admin --password password2 --authenticationDatabase admin --quiet --eval "db.getSiblingDB('testdb').users.countDocuments()"') do set USER_COUNT=%%i
    echo [INFO] Target users count: !USER_COUNT!
    
) else (
    echo [ERROR] Target database is not accessible
)

REM 检查 mongosync 工具
echo.
echo [INFO] 5. Checking mongosync tool...
docker-compose -f docker-compose-fixed.yml --profile sync run --rm mongosync mongosync --version >nul 2>&1
if %errorlevel% equ 0 (
    echo [SUCCESS] mongosync tool is available
    for /f "tokens=*" %%i in ('docker-compose -f docker-compose-fixed.yml --profile sync run --rm mongosync mongosync --version') do set VERSION=%%i
    echo [INFO] mongosync version: !VERSION!
) else (
    echo [ERROR] mongosync tool is not available
)

REM 检查日志
echo.
echo [INFO] 6. Recent container logs...
echo Source database logs (last 10 lines):
docker-compose -f docker-compose-fixed.yml logs --tail=10 mongodb-source

echo.
echo Target database logs (last 10 lines):
docker-compose -f docker-compose-fixed.yml logs --tail=10 mongodb-target

echo.
echo [INFO] Diagnosis completed!
echo.
echo [INFO] Common issues and solutions:
echo 1. If containers are not running: docker-compose -f docker-compose-fixed.yml up -d
echo 2. If network issues: docker-compose -f docker-compose-fixed.yml down ^&^& docker-compose -f docker-compose-fixed.yml up -d
echo 3. If authentication fails: check username/password in scripts
echo 4. If oplog issues: ensure replica set is properly initialized
echo 5. If sync fails: check mongosync version compatibility

pause 